package com.future.common.security.config;

import com.future.common.api.auth.StatusUser;
import com.future.common.api.content.SecurityConstant;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Component
/**
 * @ClassName : StatusTokenEnhancer
 * @Author : lby
 * @Date: 2021/5/6 21:39
 * @Description : Token 增强
 */
public class StatusTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        if (SecurityConstant.CLIENT_CREDENTIALS
                .equals(authentication.getOAuth2Request().getGrantType())) {
            return accessToken;
        }

        final Map<String, Object> additionalInfo = new HashMap<>(8);
        StatusUser smartStatusUser = (StatusUser) authentication.getUserAuthentication().getPrincipal();
        additionalInfo.put(SecurityConstant.DETAILS_USER_ID, smartStatusUser.getId());
        additionalInfo.put(SecurityConstant.DETAILS_USERNAME, smartStatusUser.getUsername());
        additionalInfo.put(SecurityConstant.DETAILS_DEPT_ID, smartStatusUser.getDeptId());
        additionalInfo.put(SecurityConstant.DETAILS_TENANT_ID, smartStatusUser.getTenantId());
        additionalInfo.put(SecurityConstant.DETAILS_LICENSE, SecurityConstant.smartStatus_LICENSE);
        additionalInfo.put(SecurityConstant.ACTIVE, Boolean.TRUE);
        additionalInfo.put(SecurityConstant.SYS_USER, smartStatusUser.getSysUser());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
